﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Drawing;
using System.Threading;

namespace Pinger
{
	public class Host
	{
		public Int64 ID { get; set; }
		public string Name { get; set; }
		public string Description { get; set; }
		public bool Busy { get { return _busy; } set { _busy = value; } }
		public ListViewItem ListViewItem { get; private set; }
		public Ping Pinger { get; private set; }

		public DateTime? LastTimeOK { get; set; }
		public string Status { get; set; }
		public long? PingTime { get; set; }
		public string Origin { get; set; }
		public Color Color { get; set; }

		private volatile bool _busy;

		public Host()
		{
		}

		public void CleanUp()
		{
			Pinger.SendAsyncCancel();
			Pinger.Dispose();
			Pinger = null;
			ListViewItem.Tag = null;
			ListViewItem = null;
			//while (Busy)
			//{
			//    Thread.Sleep(100);
			//    Console.WriteLine("Waiting for [{0}]", Name);
			//}
		}

		internal void ClearDisplayData()
		{
			Status = "";
			Origin = "";
			PingTime = null;
			//Color = System.Drawing.SystemColors.WindowText;
			Color = System.Drawing.SystemColors.Window;
		}

		internal void Init(ListViewItem item)
		{
			if (string.IsNullOrEmpty(Description))
			{
				Description = Name;
			}
			this.ListViewItem = item;
			this.Pinger = new Ping();
			item.Tag = this;
		}

		public override string ToString()
		{
			return string.Format("[{0}] ({1})", Description, Name);
		}

	}
}
